package com.zj.small.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zj.small.pojo.EmailRegister;
import com.zj.small.pojo.Result;
import com.zj.small.pojo.User;
import com.zj.small.service.SendEmail;
import com.zj.small.service.UserService;
import com.zj.small.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api")
public class EmailController {
    @Autowired
    private UserService userService;

    @Autowired
    private SendEmail sendEmail;
    private String nowCaptcha;

    private long startTime;

    private long endTime;
    // 发送验证码
    @PostMapping("/sendEmail")
    public Result sendEmail(@RequestBody String email) throws JsonProcessingException {
        // 使用 Jackson 解析 JSON 数据
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(email);

        // 获取 email 的值
        email = jsonNode.get("email").asText();

        if (email == null || email.trim().equals("")){
            return Result.error("邮箱不能为空");
        }else {
            nowCaptcha = sendEmail.sendMessage(email);
            startTime = System.currentTimeMillis();
            return Result.success();
        }
    }

    // 邮箱登录
    @PostMapping("/emailLogin")
    public Result emailLogin(@RequestBody Map<String, String> payload) {
        String email = payload.get("email");
        String captcha = payload.get("captcha");
        System.out.println(email);
        System.out.println(captcha);
        if (email == null || email.trim().equals("") || captcha == null || captcha.trim().equals("")){
            return Result.error("邮箱或验证码不能为空");
        }
        else {
            User user = userService.findByUserEmail(email);
            endTime = System.currentTimeMillis();
            if(user != null && nowCaptcha.equals(captcha) && endTime - startTime <= 120000){
                return Result.success();
            }else{
                return Result.error("邮箱或验证码错误");
            }
        }
    }
    // 邮箱注册
     @PostMapping("/emailRegister")
     public Result emailRegister(@RequestBody EmailRegister emailRegister)  {
         String email = emailRegister.getEmail();
         String captcha = emailRegister.getCapt();
         String password = emailRegister.getPassword();
         String username = emailRegister.getUsername();
         User user = userService.findByUserEmail(email);
         // 判断是否注册过
         if (email == null || email.trim().equals("") || captcha == null || captcha.trim().equals("")||username == null || username.trim().equals("")||password == null || password.trim().equals("")){
             return Result.error("注册信息不能为空");
         } else if (user!= null) {
                return Result.error("该邮箱已被注册");
         } else {
             endTime = System.currentTimeMillis();
             if(nowCaptcha.equals(captcha) && endTime - startTime <= 120000){
                 String pw = Md5Util.StringInMd5(password);
                 userService.addUser(username,pw,email);
                 return Result.success();
             }else{
                 return Result.error("邮箱或验证码错误");
             }
         }
     }
}
